import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';

enum StoreKeys {
  booInfo,
  token,
}

class Store {
  static late StoreKeys storeKeys;
  late final SharedPreferences _store;
  static Future<Store> getInstance() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return Store._internal(prefs);
  }

  Store._internal(this._store);

  getString(StoreKeys key) async {
    return _store.get(key.toString());
  }

  setString(StoreKeys key, String value) async {
    return _store.setString(key.toString(), value);
  }

  getStringList(StoreKeys key) async {
    return _store.getStringList(key.toString());
  }

  setStringList(StoreKeys key, List<String> value) async {
    return _store.setStringList(key.toString(), value);
  }

  /// 根据key存储Map类型
  Future<bool> setMap(StoreKeys key, Map value) {
    return _store.setString(key.toString(), json.encode(value));
  }

  /// 根据key获取Map类型
  getMap(StoreKeys key) {
    String jsonStr = _store.getString(key.toString()) ?? "";
    return jsonStr.isEmpty ? Map : json.decode(jsonStr);
  }

  /// 删除持久化数据中某个key
  Future<bool> remove(StoreKeys key) async {
    return await _store.remove(key.toString());
  }

  /// 清除所有持久化数据
  Future<bool> clear() async {
    return await _store.clear();
  }

}
